package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.pojo.EduCourse;
import com.atguigu.eduservice.pojo.EduCourseDescription;
import com.atguigu.eduservice.pojo.frontvo.CourseFrontVo;
import com.atguigu.eduservice.pojo.frontvo.CourseWebVo;
import com.atguigu.eduservice.pojo.vo.CourseInfiVo;
import com.atguigu.eduservice.pojo.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-20
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    //注入小节和章节
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    @Override
    public String saveCourseInfo(CourseInfiVo courseInfiVo) {
        //1.向课程表添加课程的基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfiVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        //返回值，影响行数
        if (insert<=0){
            //添加失败
            throw new GuliException(20001,"添加课程信息失败");
        }

        //获取添加之后的课程id
        String cid = eduCourse.getId();


        //2.向课程的简介表中添加课程的简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //手动设置描述的id
        eduCourseDescription.setId(cid);
        eduCourseDescription.setDescription(courseInfiVo.getDescription());
        boolean save = eduCourseDescriptionService.save(eduCourseDescription);
        if (!save){
            throw new GuliException(20001,"添加课程简介信息失败");
        }
        return cid;
    }

    @Override
    public CourseInfiVo getCourseInfo(String courseId) {
        //1.查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfiVo courseInfiVo = new CourseInfiVo();
        BeanUtils.copyProperties(eduCourse,courseInfiVo);

        //2.查询课程描述表
        EduCourseDescription cou = eduCourseDescriptionService.getById(courseId);
        courseInfiVo.setDescription(cou.getDescription());

        return courseInfiVo;
    }
//修改课程信息
    @Override
    public void updateCourseInfo(CourseInfiVo courseInfiVo) {
        //1.修改课程信息
        EduCourse eduCourse= new EduCourse();
        BeanUtils.copyProperties(courseInfiVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update==0){
            throw new GuliException(20001,"修改课程信息失败");

        }
        //修改简介表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfiVo.getId());
        description.setId(courseInfiVo.getDescription());
        eduCourseDescriptionService.updateById(description);
        
    }
//根据课程id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }
//删除课程接口
    @Override
    public void removeCourse(String courseId){
        //1.根据课程id先去删除课程的小节
        eduVideoService.removeVideoByCourseId(courseId);
        //2.根据课程id删除章节
        eduChapterService.removeChapterByCourseId(courseId);
        //3.根据课程id删除描述
        eduCourseDescriptionService.removeById(courseId);
        //4.删除课程本身
        int result = baseMapper.deleteById(courseId);
            if (result==0){//失败的返回
                throw new GuliException(20001,"删除失败");
            }
            
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam,
                                                  CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
       //判断条件值是否为空
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        //二级分类
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            //一级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        //关注度排序
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){

            wrapper.orderByDesc("buy_count");
        }

        //根据时间
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){

            wrapper.orderByDesc("gmt_create");
        }

        //根据价格
        boolean a = StringUtils.isEmpty(courseFrontVo.getPriceSort());
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())){

            wrapper.orderByDesc("price");
        }

        //方法实现
        baseMapper.selectPage(pageParam,wrapper);

        //把分页的数据放在map集合中
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;

    }
//根据课程id查询课程的基本信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {


        return baseMapper.getBaseCourseInfo(courseId);
    }

}
